import java.awt.FileDialog;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.simplify.DouglasPeuckerLineSimplifier;

public class GpsManager {
	static final String msgVersion = "GpsGen 0.1";
	static GpsTrack track_original;
	GpsTrack track;
	static GpsTrack wayPoints;
	private double dmini_meter;

	public GpsManager() {

	}

	void doSave (String fileName) {
		GpsWriter.writeGpx(track, fileName);
	}

	void readGpx(String filename, String dir) {
		String fnGpxfile = dir + filename;
		System.out.println("*** Input GPX file: " + fnGpxfile);
		if (filename.toLowerCase().endsWith(".gpx")) {
			filename = filename.substring(0, filename.length() - 4);
		}
		GpsReader gpsRdr = new GpsReader(fnGpxfile);
		track_original = new GpsTrack(gpsRdr);
		track_original.meta.name = filename;
		track_original.meta.author = "Adrian Herzog";
		track = track_original.copy();
		wayPoints = new GpsTrack(gpsRdr.wayPts);
	}

	void writeKml(boolean compressed, String fileName) {
		track.meta.desc = track.printKmlStatistics() + "; DP " + Util.format(dmini_meter, 2);
		GpsWriter.writeKml(track, fileName, compressed);
	}

	void extract(int first, int last) {
		GpsTrack track_temp;
		track_temp = track.part(first, last);
		track = track_temp;
	}

	void doPlausi(double vmaxmax) {
		if (vmaxmax == 0) vmaxmax = 2.0 * track.totalSpeed();
		System.out.println("Plausibility with max. speed " + vmaxmax);
		System.out.println("total Speed: " + track.totalSpeed());
		double vmax = Math.min(2.0 * track.totalSpeed(), vmaxmax);
//		track.reduceToPlausibleSpeeds(vmax);
		System.out.println(track.toString());
		track = GpsModeler.reduceToPlausibleSpeeds(track, vmax);
		System.out.println(track.toString());
	}

	void doPlausiPause(double vmin) {
		if (vmin == 0) vmin = track.totalSpeed() / 5.0;
		System.out.println("Plausibility with min. speed " + vmin);
		System.out.println("total Speed: " + track.totalSpeed());
		double vmin2 = Math.max(track.totalSpeed() / 10.0, vmin);
		System.out.println(track.toString());
		track = GpsModeler.reduceToPlausibleSpeedsPause(track, vmin2);
		System.out.println(track.toString());
	}

	void generalizeDP(double dmini) {
		System.out.println(track.meta + " ");
		track = GpsModeler.generalizeDP(track, dmini);
		System.out.println(track.meta + " ");
	}

	void generalizeDP0(double dmini) {
		CoordinateTime[] coord = Util.path2coordTime(track);
		DouglasPeuckerLineSimplifier dpls = new DouglasPeuckerLineSimplifier(coord);
		dpls.setDistanceTolerance(dmini);
		Coordinate[] coord_gen = dpls.simplify();
		String name = track.meta.name;
		String desc = track.meta.desc;
		String author = track.meta.author;
		String email = track.meta.email;
		String keywords = track.meta.keywords;
		track = Util.coordTime2path(coord_gen, coord);
		track.meta.author = author;
		track.meta.email = email;
		track.meta.keywords = keywords;

		track.addDesc("DP " + Util.format(dmini_meter, 2));
		track.meta.name += "_g";
	}

	void doPlausiMany(double dmini_meter_0) {
		double dmini = Util.meter2angle() * dmini_meter_0;
		System.out.println("dmini start: "
				+ dmini_meter_0 + " meter,  " + dmini + " radians.");
		if (dmini > 0.0) {
			System.out.println("\nLh\tL\tdmini\tN\tup\tdown\tchanges");
			double dist_h = Math.round(track.getLength()) +
			5.0 * track.ups - 2.0 * track.downs;
			System.out.println(Math.round(dist_h) + "\t" + Math.round(track.getLength())
					+ "\t" + 0.0 + "\t" + track.nPoints() 
					+ "\t" + track.ups + "\t" + track.downs + "\t" + track.n_changes);
			double fac = Math.sqrt(2.0);
			do {
				CoordinateTime[] coord = Util.path2coordTime(track);
				DouglasPeuckerLineSimplifier dpls = new DouglasPeuckerLineSimplifier(coord);
				dpls.setDistanceTolerance(dmini);
				Coordinate[] coord_gen = dpls.simplify();
				track = Util.coord2path(coord_gen);
				track.doStatistics();
				dist_h = Math.round(track.getLength()) +
				5.0 * track.ups - 2.0 * track.downs;
				System.out.println(Math.round(dist_h) + "\t" + Math.round(track.getLength())
						+ "\t" + Util.format(dmini_meter_0, 3) + "\t" + track.nPoints() 
						+ "\t" + Util.format(track.ups, 0) 
						+ "\t" + Util.format(track.downs, 0) + "\t" + track.n_changes);
				dmini_meter_0 *= fac;
				dmini = dmini_meter_0 * Util.meter2angle();
			} while (track.nPoints() > 3);
		}
	}


}
